<template>
    <div class="category_search">
        <el-form :inline="true" :model="searchForm" class="demo-form-inline">
            <el-form-item>
                <el-autocomplete
                        class="inline-input"
                        v-model="input"
                        :clearable="true"
                        :fetch-suggestions="querySearch"
                        placeholder="请输入内容"
                        :trigger-on-focus="false"
                        @select="handleSelect"
                ></el-autocomplete>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="search">查询</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import {log,buildParams} from '@/assets/js/util.js'
    export default{
        props:['page','bus'],
        name: 'category_search',
        data () {
            return {
                msg: 'category_search',
                label:{
                    tname:'标签查询',
                    cname:'目录查询',
                },
                searchForm:{
                    input:'',
                },
                values:[],
                input:'',
            }
        },
        methods:{
            search:function () {
                this.bus.$emit('sendKey',this.input)
            },
            handleSelect(item) {
                log(item)
            },
            querySearch(queryString, cb){
                this.values = []
                var params  = []
                params[0] = 'keyword='+queryString
                if(this.page = 'cates'){
                    params[1] = 'type=1'
                }else{
                    params[1] = 'type=2'
                }
                var url = server_url+'/api/admin/category/search'
                url = url + buildParams(params)
                this.$http.jsonp(url, {}, {
                    headers: {},
                    emulateJSON: true
                }).then(function(response) {
                    var data = response.data.data
                    console.log(data)
                    for(var i = 0; i<data.length; i++){
                        var obj = {}
                        obj.value = data[i].name
                        obj.id    = data[i].id
                        this.values.push(obj)
                    }
                    if(this.values.length>0){
                        cb(this.values)
                    }else{
                        cb([])
                    }
                }, function(response) {
                    console.log(response)
                });
            },
        }
    }
</script>

<style>
    .category_search{
    }
</style>